﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace SkypeTranslator.Utilities
{
	public static class SystemResources
	{
		private static PerformanceCounter _memoryPerformanceCounter;
		private static readonly object _syncRoot = new object();
		private static PerformanceCounter MemoryPerformanceCounter
		{
			get
			{
				if (SystemResources._memoryPerformanceCounter == null)
				{
					object syncRoot;
					Monitor.Enter(syncRoot = SystemResources._syncRoot);
					try
					{
						if (SystemResources._memoryPerformanceCounter == null)
						{
							SystemResources._memoryPerformanceCounter = new PerformanceCounter("Memory", "Available Bytes");
						}
					}
					finally
					{
						Monitor.Exit(syncRoot);
					}
				}
				return SystemResources._memoryPerformanceCounter;
			}
		}
		public static long GetAvailableMemory(SizeUnits units)
		{
			long num = Convert.ToInt64(SystemResources.MemoryPerformanceCounter.NextValue());
			if (units == SizeUnits.Bytes)
			{
				return num;
			}
			if (units == SizeUnits.Kilobytes)
			{
				return num / 1024L;
			}
			if (units == SizeUnits.Megabytes)
			{
				return num / 1048576L;
			}
			return num / 1073741824L;
		}
	}
}
